<template>
	<view class="content">
		<view class="user-photo-block">
			<image :src="form.faceUrl?form.faceUrl:imageUrl+'/job_hunting/home/Mr.png'" class="user-photo"></image>
			<view class="darkgray fs12 mt30">数据表明招聘者对真实头像更有好感</view>
			<button hover-class="none" class="fs12 mt30" style="color: #FE782B;" @click="openItem">上传头像</button>
		</view>
		<view class="form-block" style="border-radius:30rpx 30rpx 0 0 ;">
			<view class="form-title">基本信息</view>
			<view class="form-item">
				<view class="form-label">真实姓名</view>
				<view class="form-content">
					<input v-model="form.realName" maxlength="10" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写姓名" class="form-ctrl" />
					<view class="iconfont icon-gengduAo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">性别</view>
				<view class="form-content">
					<picker class="form-ctrl" :range="gender" range-key="value" :value="genderIndex"
						@change="bindGender">{{gender[genderIndex]['value']}}</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">出生日期</view>
				<view class="form-content">
					<picker mode="date" class="form-ctrl" fields="day" :value="form.birthday" :end="birthdayEnd"
						@change="bindBirthday">{{form.birthday}}</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
		</view>
		<view class="form-block" style="border-radius:30rpx 30rpx 0 0 ;">
			<view class="form-title">联系方式</view>
			<view class="form-item">
				<view class="form-label">邮箱</view>
				<view class="form-content">
					<input v-model="form.eml" maxlength="30" type="text" placeholder-class="inputPlaceholder"
						placeholder="选填" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">微信号</view>
				<view class="form-content">
					<input v-model="form.weChat" maxlength="30" type="text" placeholder-class="inputPlaceholder"
						placeholder="选填" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view @click="handleSave" class="btn">保存</view>
		</view>
		<view v-if="displayChoose" class="choose-block">
			<view class="choose-content">
				<view class="choose">
					<view class="correct">
						<view class="text-item">
							<view class="title">
								<view class="iconfont icon-icon_xialaxuanze"></view>正确示例
							</view>
							<view class="text">好的形象会让HR更青睐</view>
						</view>
						<image :src="imageUrl+'/avatar/1正确示例.png'" class="choose-photo"></image>
					</view>
					<view class="wrong-item">
						<view class="title">
							<view class="iconfont icon-guanbi"></view>错误示例
						</view>
						<view class="wrong-img">
							<view class="img-item">
								<image :src="imageUrl+'/avatar/2非人物照.png'" class="choose-photo"></image>
								<view class="text">非人物照</view>
							</view>
							<view class="img-item">
								<image :src="imageUrl+'/avatar/3五官遮挡.png'" class="choose-photo"></image>
								<view class="text">五官遮挡</view>
							</view>
							<view class="img-item">
								<image :src="imageUrl+'/avatar/4模糊不清.png'" class="choose-photo"></image>
								<view class="text">模糊不清</view>
							</view>
							<view class="img-item">
								<image :src="imageUrl+'/avatar/5衣着不当.png'" class="choose-photo"></image>
								<view class="text">衣着不当</view>
							</view>
						</view>
					</view>
				</view>
				<button hover-class="none" @chooseavatar="photoChoose" open-type="chooseAvatar" class="uploading"
					style="margin-bottom: 20rpx;">上传头像</button>
				<view class="cancel" @click="cancel">取消</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserCustomer,
		submitUserCustomer
	} from '@/api/resume.js'
	import {
		uploadFile
	} from '@/api/file.js'
	import {
		validate
	} from '@/util/validate.js'
	import {
		getYearsAgo
	} from '@/util/util.js'
	export default {
		data() {
			return {
				imageUrl: '',
				displayChoose: false,
				form: {
					realName: '',
					sex: null,
					birthday: getYearsAgo(20),
					eml: '',
					weChat: '',
					faceUrl: ''
				},
				rules: {
					realName: {
						name: '姓名',
						required: true,
						validator: function(value, form, callback) {
							if (value.length < 2) {
								callback('请填写正确的姓名')
							}
							callback();
						}
					},
					faceUrl: {
						name: '头像',
						required: true
					},
				},
				gender: [{
						id: 1,
						value: '男'
					},
					{
						id: 2,
						value: '女'
					}
				],
				genderIndex: 0,
				birthdayEnd: '',
			}
		},
		methods: {
			openItem() {
				this.displayChoose = true
			},
			cancel() {
				this.displayChoose = false
			},
			getDetail() {
				uni.showLoading({
					title: '加载中',
					mask: true,
				})
				getUserCustomer().then((res) => {
					uni.hideLoading()
					res.birthday = res.birthday || this.birthdayEnd
					this.form = res
					this.form.realName = res.realName? res.realName : ''
					this.genderIndex = res.sex ? res.sex - 1 : 0;
				}).catch(() => {
					uni.hideLoading()
					this.showToast('加载失败', () => {
						uni.navigateBack()
					})
				})
			},
			bindGender(e) {
				this.genderIndex = e.detail.value;
				this.form.sex = e.detail.value + 1;
			},
			bindBirthday(e) {
				this.form.birthday = e.detail.value
			},
			photoChoose(e) {
				const filePath = e.detail.avatarUrl;
				uni.showLoading({
					title: '上传中',
					mask: true
				})
				this.uploadFace(filePath);
			},
			uploadFace(filePath) {
				uploadFile(filePath).then((res) => {
					const data = JSON.parse(res.data);
					if (data.code == 200) {
						this.form.faceUrl = data.data
						this.showToast('上传成功')
						uni.hideLoading()
						this.displayChoose = false
					} else {
						this.showToast(data.msg)
					}

				}).catch((res) => {
					uni.hideLoading()
					this.showToast('上传失败');
				})
			},
			handleSave() {
				let form = JSON.parse(JSON.stringify(this.form));
				form.sex = this.gender[this.genderIndex].id
				uni.showLoading({
					title: '提交中',
					mask: true
				})
				if (validate(form, this.rules)) {
					submitUserCustomer(form).then((res) => {
						uni.$emit('refresh', 0)
						this.$store.dispatch('user/getUser').then(() => {
							uni.hideLoading()
							this.showToast('保存成功', () => {
								uni.switchTab(
								    {
								      url: '/pages/mine/index'
								    }
								)
							})
						}).catch(() => {
							uni.hideLoading()
							this.showToast('保存失败');
						})
					}).catch(() => {
						uni.hideLoading()
						this.showToast('保存失败');
					})
				}
			}
		},
		onLoad() {
			this.getDetail();
			this.imageUrl = this.globalConfig.imageUrl
			this.birthdayEnd = getYearsAgo(16)
		}
	}
</script>

<style lang="scss">
	page {
		background: #F9F9F9;
	}

	.content {
		width: 100%;

		.btn {
			width: 350rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #FF7626;
			color: #fff;
			font-size: 28rpx;
			border-radius: 40rpx;
			margin: 0 auto;
			margin-top: 30rpx;
		}

		.user-photo-block {
			width: 100%;
			height: 450rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.user-photo {
				width: 180rpx;
				height: 180rpx;
				border-radius: 50%;
			}
		}

		.form-block {
			width: 100%;
			box-shadow: 0 0 10px 0 #efefef;
			background: #fff;
			padding: 30rpx;

			.form-title {
				font-size: 36rpx;
				font-weight: bold;
			}

			.form-item {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #efefef;
				padding-top: 30rpx;

				.form-label {
					font-size: 28rpx;
					color: #999;
				}

				.form-content {
					display: flex;
					flex-direction: row;
					align-items: center;

					.form-ctrl {
						height: 100rpx;
						line-height: 100rpx;
						flex-grow: 1;
					}

					.arrow {
						flex-grow: 0;
						color: #999;
						font-size: 24rpx;
					}
				}
			}
		}

		.choose-block {
			position: fixed;
			left: 0;
			top: 0;
			background: rgba(0, 0, 0, 0.2);
			z-index: 999;
			width: 100vw;
			height: 100vh;

			.choose-content {
				width: 100%;
				height: auto;
				background: #fff;
				z-index: 9999;
				position: fixed;
				transform: translate(-50%, 0%);
				left: 50%;
				bottom: 0;
				display: flex;
				flex-direction: column;
				box-shadow: 0 0 10px 0px #dadada;

				.choose {
					display: flex;
					flex-direction: column;
					padding: 30rpx;
					background-color: #F6F6F6;

					.title-item {
						display: flex;
						flex-direction: row;
						justify-content: space-between;
						margin-bottom: 30rpx;
						padding-bottom: 20rpx;
						border-bottom: 1rpx solid #dadada;

						.choose-title {
							font-size: 36rpx;
							font-weight: bold;
						}
					}

					.choose-item {
						margin-top: 10rpx;
						font-size: 30rpx;
						line-height: 50rpx;
						margin-bottom: 50rpx;
					}

				}

				.btn1 {
					margin-top: 50rpx;
					height: 60rpx;
					width: 300rpx;
					border-radius: 20rpx;
					margin-bottom: 30rpx;
				}
			}
		}

		.choose-block {
			.choose-content {
				width: 100%;
				background: #F6F6F6;

				.choose {
					padding: 30rpx;
					display: flex;
					flex-direction: column;

					.correct {
						display: flex;
						flex-direction: row;
						justify-content: space-between;
						align-items: center;
						margin-top: 20rpx;

						.text-item {
							display: flex;
							flex-direction: column;

							.title {
								font-weight: bold;
								margin-bottom: 20rpx;
								display: flex;
								flex-direction: row;
								font-size: 30rpx;
								align-items: center;

								.iconfont {
									color: green;
									margin-right: 10rpx;
									font-size: 26rpx;
								}
							}

							.text {
								font-size: 26rpx;
							}
						}

						.choose-photo {
							width: 120rpx;
							height: 120rpx;
						}
					}

					.wrong-item {
						display: flex;
						flex-direction: column;
						margin-top: 50rpx;
						margin-bottom: 20rpx;

						.title {
							font-weight: bold;
							margin-bottom: 30rpx;
							display: flex;
							flex-direction: row;
							font-size: 30rpx;
							align-items: center;

							.iconfont {
								color: red;
								margin-right: 10rpx;
								font-size: 26rpx;
							}
						}

						.wrong-img {
							display: flex;
							flex-direction: row;
							justify-content: space-around;

							.img-item {
								display: flex;
								flex-direction: column;

								.choose-photo {
									width: 120rpx;
									height: 120rpx;
									margin-bottom: 10rpx;
								}

								.text {
									font-size: 25rpx;
									color: #999;
								}
							}
						}
					}
				}
			}

			.uploading {
				height: 110rpx;
				width: 100%;
				margin-top: 30rpx;
				text-align: center;
			}

			.cancel {
				height: 100rpx;
				width: 100%;
				text-align: center;
				font-size: 36rpx;
				align-items: center;
			}
		}
	}
</style>
